﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Component.Base;
using System.Collections.Generic;
using HRProject.Code.DTO;

namespace HRProject.Department
{
    public partial class RoleInDepartment : System.Web.UI.Page
    {
        private BLDepartment company;
        private List<BLRole> departmentRoles;
        private Guid departmentGuid;

        public List<BLRole> DepartmentRoles
        {
            get { return departmentRoles; }
        }

        public BLDepartment Company
        {
            get { return company; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            company = getCompany();
            string departmentId = Request.Params["departmentId"];
            departmentGuid = Guid.Empty;
            try
            {
                departmentGuid = new Guid(departmentId);
            }
            catch(Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
            }
            RoleDAO dao = new RoleDAO();
            BLRole filter = new BLRole();
            filter.Department = new BLDepartment();
            filter.Department.Id=departmentGuid;
            filter.Id=Guid.Empty;
            filter.Name=string.Empty;
            departmentRoles = dao.searchRolesInDepartment(filter);
            for (int i = 0; i < departmentRoles.Count; i++)
            {
                if (!departmentGuid.Equals(departmentRoles[i].Department.Id))
                {
                    departmentRoles.RemoveAt(i);
                    i--;
                }
            }
        }

        private BLDepartment getCompany()
        {
            DepartmentDAO departmentDAO = new DepartmentDAO();
            BLDepartment result = departmentDAO.getRootDepartment();
            return result;
        }
        
        public void printDepartmentNode(BLDepartment department)
        {
            string pre = "<div class=\"line\" onmousemove=\"treeNodeMove(this)\" onmouseout=\"treeNodeOut(this)\">";
            string selected = "";
            if (!Guid.Empty.Equals(department.Id) && department.Id.Equals(departmentGuid))
            {
                selected = " selected";
            }
            string line = "<div class=\"node"+selected+"\" onclick=\"viewRolesInDepartment(this)\"><a href=\"javascript:void(0);\">" + department.Name + "</a></div>";
            string bar = "<div class=\"actionbar\">";
            if (!Guid.Empty.Equals(department.Id))
            {
                bar += "<div class=\"item add\" onclick=\"addRoleToDepartment(this)\"></div>";
            }
            bar += "<input type=\"hidden\" name=\"id\" value=\"" + department.Id + "\"/>";
            bar += "<input type=\"hidden\" name=\"name\" value=\"" + department.Name + "\"/>";
            bar += "</div>";
            if (department.Parent != null)
            {
                line = "<div class=\"pad\"></div>" + line;
                line = "<div class=\"branch direct\"></div>" + line;
            }
            BLDepartment ancestor = department.Parent;
            while (ancestor != null)
            {
                if (ancestor.Parent!=null)
                {
                    if (ancestor.Parent.Children.IndexOf(ancestor) < (ancestor.Parent.Children.Count - 1))
                    {
                        line = "<div class=\"tab\"></div>" + line;
                        line = "<div class=\"branch\"></div>" + line;
                    }
                    else
                    {
                        line = "<div class=\"tab\"></div>" + line;
                        line = "<div class=\"branch empty\"></div>" + line;
                    }
                }
                ancestor = ancestor.Parent;
            }
            string suf = "</div>";
            Response.Write(pre + line + bar + suf);

            foreach (BLDepartment child in department.Children)
            {
                printDepartmentNode(child);
            }
        }
    }
}
